// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
const COIN = [10,100,500,1000,1500]
@ccclass
export default class HorseRace extends cc.Component {

    @property(cc.Node)
    resultPanel: cc.Node = null;
    @property(cc.Node)
    horseList: cc.Node = null;
    @property(cc.Node)
    coinList: cc.Node = null;
    // update (dt) {}
    currentSelectedHorse: number = 0;
    currentSelectedCoin: number = 0;
    currentBet = [0,0,0,0,0];
    protected onLoad(): void {
        cc.game.once('RaceEnd', this.raceEnd, this)
        this.initHorseList();
    }

    initHorseList(){
        this.horseList.children.forEach((item,index)=>{
            item.children[0].children[0].getComponent(cc.Label).string = "0";
        })
    }

    onBetClick(event,index){
        this.currentBet[this.currentSelectedHorse] += COIN[this.currentSelectedCoin];
        this.updateBetLab();
    }

    updateBetLab(){
        this.horseList.children[this.currentSelectedHorse].children[0].children[0].getComponent(cc.Label).string = this.currentBet[this.currentSelectedHorse].toString();
    }

    onSelectHorseClick(event,index){
        this.currentSelectedHorse = index;
        this.horseList.children.forEach((item,index)=>{
            item.scale = 1;
        })
        this.horseList.children[index].scale = 1.2;
    }
    onSelectCoinClick(event,index){
        this.currentSelectedCoin = index;
        this.coinList.children.forEach((item,index)=>{
            item.scale = 1;
        })
        this.coinList.children[index].scale = 1.2;
    }

    raceEnd(){
        console.log('RaceEnd')
        this.resultPanel.active = true;
    }

    raceStart(){
        console.log('RaceStart')
        cc.game.emit('RaceStart');
    }

    restart(){
        cc.game.resume();
        cc.director.loadScene('horseRace');
        //cc.game.restart();
    }
}
